package Blog;

import java.util.*;

public class MyBinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root==null)return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root==null)return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    //获取节点个数
    public int size(TreeNode root) {
        if(root==null) return 0;
        return size(root.left)+size(root.right)+1;
    }

    public int nodeSize;
    public void size2(TreeNode root) {
        if(root==null) return;
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }

    public int leafNodeSize(TreeNode root) {
        if(root==null) return 0;
        if(root.left==null && root.right==null) return 1;
        return leafNodeSize(root.left)+leafNodeSize(root.right);
    }

    public int leafNodeCount;
    public void leafNodeSize2(TreeNode root) {
        if(root==null) return;
        if(root.left==null && root.right==null) leafNodeCount++;
        leafNodeSize2(root.left);
        leafNodeSize2(root.right);
    }

    public int getKLevelNode(TreeNode root,int k) {
        if(root==null) return 0;
        if(k==1) return 1;
        return getKLevelNode(root.left,k-1)+getKLevelNode(root.right,k-1);
    }

    public int getHeight(TreeNode root) {
        if(root==null) return 0;
        int leftH=getHeight(root.left);
        int rightH=getHeight(root.right);
        return Math.max(leftH,rightH)+1;
    }

    public TreeNode find(TreeNode root,int val) {
        if(root==null) return null;
        if(root.val==val) return root;
        TreeNode left=find(root.left,val);
        if(left!=null) return left;
        TreeNode right=find(root.right,val);
        if(right!=null) return right;
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if(root==null) return;//若是空树,直接返回
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null) queue.offer(cur.left);
            if(cur.right!=null) queue.offer(cur.right);
        }
        System.out.println();
    }

    /*
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null) return null;//若是空树,直接返回
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size=queue.size();
            List<Integer> list=new ArrayList<>();
            while(size>0) {
                TreeNode cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

     */


    public boolean isCompleteTree(TreeNode root) {
        if(root==null) return true;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            if(cur==null) break;//注意这行代码要放在下面两个offer的前面,否则会出现NullPointerException
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while(!queue.isEmpty()) {
            TreeNode tmp=queue.peek();
            if(tmp!=null) {
                return false;
            }
            queue.poll();
        }
        return true;
    }

    public void preOrderNot(TreeNode root) {
        if(root==null)return;//空树就返回
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()){
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            cur=top.right;
        }
    }

    public void inOrderNot(TreeNode root) {
        if(root==null)return;//空树就返回
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()){
            while (cur != null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }
    }

    public void postOrderNot(TreeNode root) {
        if(root==null)return;
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;
        while(cur!=null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //cur一直遍历到最左侧节点的左节点-> null
            TreeNode top = stack.peek();
            if (top.right == null || top.right==prev) {
                stack.pop();
                System.out.print(top.val + " ");//当该节点的右节点为空时才能打印根节点值
                prev=top;
            } else {
                cur = top.right;//若该节点右节点不为空,则继续进入while循环
            }
        }
    }

}
